package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.Employee;
import com.qf.bean.Menu;
import com.qf.bean.Role;
import com.qf.dao.EmployeeDao;
import com.qf.service.EmployeeService;
import com.qf.service.MenuService;
import com.qf.service.RoleService;
import com.qf.utils.constant.PageUils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 19/11/2019
 * Echo
 * qmxc
 * Alibaba
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Override
    public Employee login(String username, String password) {
        Employee employee = employeeDao.login(username);
        if (employee != null) {
            if (employee.getLoginpassword().equals(password)) {
               Role role= roleService.findByRoleId(employee.getRoleid());
                System.out.println(role);
                List<Menu> menus = menuService.findByRole(role.getRoleid());//找到该角色的所有的菜单集合

                ArrayList firstMenu= new ArrayList();//保存一级菜单
                for (Menu menu : menus) {
                    if(menu.getUpmenuid()==0){ //一级菜单
                        ArrayList secondList= new ArrayList();//保存二级菜单
                        for (Menu second : menus) {
                            if(menu.getMenuid()==second.getUpmenuid()){//如果一级菜单的id(menuid)==二级菜单的父id(upmenuid)
                                secondList.add(second);
                            }
                        }
                        //将二级菜单的集合封装到一级菜单中
                        menu.setSecondList(secondList);
                        //将一级菜单在添加到集合中
                        firstMenu.add(menu);
                    }
                }
                List<Menu> permissions= menuService.findByRole2(role.getRoleid());
                //3.将菜单赋值给角色，角色再赋值给用户
                role.setMenuList(firstMenu);
                role.setPermission(permissions);
                employee.setRole(role);
                return employee;
            }
            return null;
        }
        return null;
    }

    @Override
    public PageInfo<Employee> show(Integer index,String username,String rolename,String deptname) {
        HashMap map = new HashMap();
        map.put("username",username);
        map.put("rolename",rolename);
        map.put("deptname",deptname);
        System.out.println(PageUils.PAGESIZE.getSize()+">>>>>"+index);
        PageHelper.startPage(index, PageUils.PAGESIZE.getSize());
        List<Employee> employeeList = employeeDao.show(map);
        System.out.println(employeeList);
        PageInfo<Employee> pageInfo = new PageInfo(employeeList);
        return pageInfo;
    }

    @Override
    public int addEmp(Employee employee) {
       int add= employeeDao.addEmp(employee);
        return add;
    }

    @Override
    public Employee findNum(String numval) {
       Employee employee= employeeDao.findNum(numval);
       return employee;
    }

    @Override
    public Employee findById(Integer id) {
        Employee employee=employeeDao.findById(id);
        return employee;
    }

    @Override
    public int update(Employee employee) {
        int i=employeeDao.updateByPrimaryKeySelective(employee);
        return i;
    }

    @Override
    public int deleteById(int id) {
        return employeeDao.deleteById(id);
    }

    @Override
    public int deleteByIds(int[] empid,Integer state) {
        HashMap map = new HashMap();
        map.put("array1",empid);
        map.put("state",state);
        return employeeDao.deleteByIds(map);
    }

    @Override
    public List<Employee> findbyids(int[] empid) {
        List<Employee> list=employeeDao.findByIds(empid);
        return list;
    }
}
